﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dryricenet
{
    public partial class ph_Picture : System.Web.UI.Page
    {
        public string tx = "";
        protected void Page_Load(object sender, EventArgs e)
        {
           
           
            if (!IsPostBack)
            {
                string name = Session["Name"].ToString();
                DataTable dt = userBLL.selectNameBll(name);
                DataRow dr = dt.Rows[0];//第一条数据
                int id = Convert.ToInt32(dr["UserID"]);
                txtName.Text = dr["UserName"].ToString();
                txtAge.Text = dr["Age"].ToString();
                txtPhone.Text = dr["Phone"].ToString();
                //性别处理
                if (dr["Sex"].ToString() == "男")
                {
                    rbNan.Checked = true;
                }
                else
                {
                    rbNv.Checked = true;
                }
                //头像
                if (dr["Image"].ToString() == "")
                {
                    tx = "img/profile_small.jpg";
                }
                else {
                    tx = dr["Image"].ToString();
                }
            }
        }
        //保存
        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = Session["Name"].ToString();
            string sex = "";
            int age = 0;
            if (rbNan.Checked)
            {
                sex = "男";
            }
            else
            {
                sex = "女";
            }
            if (txtName.Text.Trim() != "" && txtAge.Text.Trim() != "" && txtPhone.Text.Trim() != "" )
            {
                age = Convert.ToInt32(txtAge.Text.Trim());
                if (age > 10 && age < 108)
                {
                    User us = new User();
                    us.UserName = txtName.Text.Trim();
                    us.Sex = sex;
                    us.Age = age;
                    us.Phone = txtPhone.Text.Trim();
                    if (userBLL.updateUserBLL(us, name))
                    {
                        Session["Name"] = txtName.Text.Trim();
                        Response.Write("<script>javascript:alert('修改成功!');history.back();</script>");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("<script>javascript:alert('修改失败!');history.back();</script>");
                        Response.End();
                    }
                }
                else
                {
                    Response.Write("<script>javascript:alert('年龄输入不合法!');history.back();</script>");
                    Response.End();
                }
            }
            else
            {
                Response.Write("<script>javascript:alert('输入不能为空,请重新输入!');history.back();</script>");
                Response.End();
            }
        }
    }
}